package j20251008;

import java.util.ArrayList;
import java.util.List;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution {
    public long prev=Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
        if(root==null) return true;
        boolean retLeft=isValidBST(root.left);
        if(prev<root.val){
            prev=root.val;
        }else{
            return false;
        }
        boolean retRight=isValidBST(root.right);
        return retLeft&&retRight;
    }

    public int ret=0;
    public int count=0;
    public int kthSmallest(TreeNode root, int k) {
        count=k;
        dfs(root);
        return ret;
    }
    public void dfs(TreeNode root){
        if(root==null) return;
        dfs(root.left);
        count--;
        if(count==0){
            ret=root.val;
            return;
        }
        dfs(root.right);
    }

    public List<String> ret1=new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        String path="";
        dfs(root,path);
        return ret1;
    }
    public void dfs(TreeNode root,String path){
        if(root==null) return;
        if(root.left==null&&root.right==null){
            path+=root.val;
            ret1.add(path);
        } else path+=root.val+"->";
        dfs(root.left,path);
        dfs(root.right,path);
    }
}
